package com.library.dcy.dao;


import com.library.dcy.entity.BorrowInfo;
import com.library.dcy.entity.RepayInfo;
import com.library.dcy.util.BaseDao;
import com.library.dcy.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 业务实现类
 */
public class BookDaoImpl extends BaseDao implements BookDao {
    String path="resources/mybatis-config.xml";
    /**
     * //按读者id查询借书记录
//     * @param id//读者id
//     * @param star//起始下标
//     * @param pageSize//查询条数
     * @return
     */
    @Override
    public List<BorrowInfo> borrow(Map map) throws Exception {
        SqlSession session = MybatisUtil.getSession(path);
        BookDao mapper = session.getMapper(BookDao.class);
        return mapper.borrow(map);
//        List<BorrowInfo> list=new ArrayList<>();
//        String sql="";
//        ResultSet resultSet = set(sql, id,star,pageSize);
//        if(resultSet!=null){
//            try {
//                while (resultSet.next()){
//                    String cardName=resultSet.getString(1);
//                    String bookName=resultSet.getString(2);
//                    Date startTime=resultSet.getTime(3);
//                    Integer day=resultSet.getInt(4);
//                    String phone=resultSet.getString(5);
//                    BorrowInfo borrowInfo=new BorrowInfo(cardName,bookName,startTime,day,phone);
//                    list.add(borrowInfo);
//                }
//            } catch (SQLException throwables) {
//                throwables.printStackTrace();
//            }
//        }
//        return list;
    }

    /**
     * 分页查询借书总记录数
     * @param id//读者id
     * @return
     */
    @Override
    public Integer borrowcount(Integer id) throws Exception {
        SqlSession session = MybatisUtil.getSession(path);
        BookDao mapper = session.getMapper(BookDao.class);
        return mapper.borrowcount(id);
//        ResultSet resultSet=null;
//        Integer i=0;
//        String sql="SELECT COUNT(*) FROM borrow w,reader r,book k where w.cardId=r.reaId AND w.bookId=k.bookId and w.cardId=?";
//        resultSet = set(sql, id);
//        if(resultSet!=null){
//            try {
//                while (resultSet.next()){
//                    i=resultSet.getInt(1);
//                }
//            } catch (SQLException throwables) {
//                throwables.printStackTrace();
//            }finally {
//                release(null,null,resultSet);
//            }
//        }
//        return i;
    }

    /**
     * //按读者id查询还书记录
//     * @param id//读者id
//     * @param star//起始下标
//     * @param pageSize//查询条数
     * @return
     */
    @Override
    public List<RepayInfo> repay(Map map) throws Exception {
        SqlSession session = MybatisUtil.getSession(path);
        BookDao mapper = session.getMapper(BookDao.class);
        return mapper.repay(map);
//        List<RepayInfo> list=new ArrayList<>();
//        String sql="select r.`name`,k.bookName,y.endTime from repay y,reader r,book k WHERE y.cardId=r.reaId AND y.bookId=k.bookId and y.id=? LIMIT ?,?";
//        ResultSet resultSet = set(sql, id,star,pageSize);
//        if(resultSet!=null){
//            try {
//                while (resultSet.next()){
//                    String cardName=resultSet.getString(1);
//                    String bookName=resultSet.getString(2);
//                    Date endTime=resultSet.getTime(3);
//                    RepayInfo repayInfo=new RepayInfo(cardName,bookName,endTime);
//                    list.add(repayInfo);
//                }
//            } catch (SQLException throwables) {
//                throwables.printStackTrace();
//            }finally {
//                release(null,null,resultSet);
//            }
//        }
//        return list;
    }

    /**
     * 分页查询还书总记录数
     * @param id//读者id
     * @return
     */
    @Override
    public Integer repaycount(Integer id) throws Exception {
        SqlSession session = MybatisUtil.getSession(path);
        BookDao mapper = session.getMapper(BookDao.class);
        return mapper.repaycount(id);
//        ResultSet resultSet=null;
//        Integer i=0;
//        String sql="select COUNT(*) from repay y,reader r,book k WHERE y.cardId=r.reaId AND y.bookId=k.bookId and y.id=?";
//        resultSet = set(sql, id);
//        if(resultSet!=null){
//            try {
//                while (resultSet.next()){
//                    i=resultSet.getInt(1);
//                }
//            } catch (SQLException throwables) {
//                throwables.printStackTrace();
//            }finally {
//                release(null,null,resultSet);
//            }
//        }
//        return i;
    }
}
